//
//  smcJaikuRSSParser.m
//  jaikuAPI
//
//  Created by Christian Martick on 12.04.07.
//  Copyright 2007 Christian Martick. All rights reserved.
//
//	jaiku api - parse rss feed
// 
//	this objects handles jaiku stream rss parsing, it subclasses NSXMLParser
//
//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcJaikuRSSParser.h"


@implementation smcJaikuRSSParser
- (void) dealloc {
	[currentEntry release];
	[currentUser release];
	[currentString release];
	[entries release];
	[super dealloc];
}


- (id) initWithContentsOfURL: (NSURL *) url {
	self = [super initWithContentsOfURL: url];
	if (self != nil) {
		isRSSFeed = NO;
		inItem = NO;
		currentString = [[NSMutableString alloc] init];
		currentUser = [[NSMutableDictionary alloc] init];
		entries = [[NSMutableArray alloc] init];
		currentEntry = [[NSMutableDictionary alloc] init];
	}
	[self setDelegate: self];
	return self;
}

- (id) initWithData: (NSData *) data {
	self = [super initWithData: data];
	if (self != nil) {
		isRSSFeed = NO;
		inItem = NO;
		currentString = [[NSMutableString alloc] init];
		currentUser = [[NSMutableDictionary alloc] init];
		entries = [[NSMutableArray alloc] init];
		currentEntry = [[NSMutableDictionary alloc] init];
	}

	[self setDelegate: self];
	return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	[currentString setString: @""];
	//if in part set flag
	if ([elementName isEqualToString: @"rss"])
		isRSSFeed = YES;
	else if ([elementName isEqualToString: @"item"])
	{
		[currentEntry removeAllObjects];
		inItem = YES;
	}
	else if ([elementName isEqualToString: @"jaiku:user"])
	{
	
		//get user information 
		NSString* data = [attributeDict objectForKey: @"nick"];
		if (data)
			[currentUser setObject: data forKey: kSmcEntryKeyNick];
			
		data = [attributeDict objectForKey: @"first_name"];
		if (data)
			[currentUser setObject: data forKey: kSmcEntryKeyFirstName];
		
		data = [attributeDict objectForKey: @"last_name"];
		if (data)
			[currentUser setObject: data forKey: kSmcEntryKeyLastName];
		
		data = [attributeDict objectForKey: @"avatar"];
		if (data)
			[currentUser setObject: [NSURL URLWithString: data] forKey: kSmcEntryKeyUserImageURL];
		
		data = [attributeDict objectForKey: @"url"];
		if (data)
		{
			//some feed error within jaiku replace \/\/# with \/\/
			NSMutableString *tmp = [NSMutableString stringWithString: data];
			[tmp replaceOccurrencesOfString: @"//#" withString: @"//" options:NSBackwardsSearch range: NSMakeRange(0, [tmp length])];
			[currentUser setObject: [NSURL URLWithString: tmp] forKey: kSmcEntryKeyUserURL];
		}
	
	} else if ([elementName isEqualToString: @"jaiku:comment"])
	{
		NSString *data = [attributeDict objectForKey: @"count"];
		if (data)
			[currentUser setObject: [NSNumber numberWithInt: [data intValue]] forKey: kSmcEntryKeyCommentCount];
	}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
	if (string != nil)
		[currentString appendString: string];
	else
		[currentString appendString: @""];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
	if ([elementName isEqualToString: @"item"])
	{
		//item done, add user and put currentEntry to array
		inItem = NO;
		[currentEntry addEntriesFromDictionary: [NSDictionary dictionaryWithDictionary: currentUser]];
		[entries addObject: [NSMutableDictionary dictionaryWithDictionary: currentEntry]];
	} else if (inItem && (![elementName isEqualToString: @"jaiku:user"]))
	{
		//add key
		
		if ([elementName isEqualToString: kSmcEntryKeyURL])
		{
			//some feed error within jaiku replace \/\/# with \/\/
			NSMutableString *tmp = [NSMutableString stringWithString: currentString];
			[tmp replaceOccurrencesOfString: @"//#" withString: @"//" options:NSBackwardsSearch range: NSMakeRange(0, [tmp length])];
			if ([NSURL URLWithString: tmp])
				[currentEntry setObject: [NSURL URLWithString: tmp] forKey: elementName];
		}
		else
			[currentEntry setObject: [NSString stringWithString: currentString] forKey: elementName];	
	}
}

- (NSArray *) entries;
{
	return entries;
}

- (BOOL) isRSSFeed
{
	return isRSSFeed;
}
@end
